package com.huike.business.service.impl;


import com.huike.business.domain.TbBusiness;
import com.huike.business.domain.TbBusinessTrackRecord;
import com.huike.business.domain.vo.BusinessTrackVo;
import com.huike.business.mapper.TbBusinessMapper;
import com.huike.business.mapper.TbBusinessTrackRecordMapper;
import com.huike.business.domain.TbBusinessTrackRecord;
import com.huike.business.mapper.TbBusinessTrackRecordMapper;
import com.huike.business.service.ITbBusinessTrackRecordService;
import com.huike.clues.domain.TbClue;
import com.huike.clues.mapper.SysDictDataMapper;
import com.huike.common.utils.SecurityUtils;
import com.huike.common.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

import java.util.List;

/**
 * 商机跟进记录Service业务层处理
 * 
 * @author wgl
 * @date 2021-04-28
 */
@Service
public class TbBusinessTrackRecordServiceImpl implements ITbBusinessTrackRecordService {

    @Autowired
    private TbBusinessMapper tbBusinessMapper;
    @Autowired
    private TbBusinessTrackRecordMapper tbBusinessTrackRecordMapper;
    @Autowired
    private SysDictDataMapper dictDataMapper;

    @Override
    public void add(BusinessTrackVo businessTrackVo, String username, Date createDate) {
        TbBusiness tbBusiness = new TbBusiness();
        TbBusinessTrackRecord tbBusinessTrackRecord = new TbBusinessTrackRecord();
        BeanUtils.copyProperties(businessTrackVo,tbBusiness);
        BeanUtils.copyProperties(businessTrackVo,tbBusinessTrackRecord);

        System.out.println(businessTrackVo);
        System.out.println(username);
        System.out.println(createDate);
        System.out.println(tbBusiness.toString());
        System.out.println(tbBusinessTrackRecord.toString());
        tbBusiness.setCreateBy(username);
        tbBusiness.setCreateTime(createDate);
        tbBusiness.setStatus(TbClue.StatusType.FOLLOWING.getValue());
        tbBusiness.setId(businessTrackVo.getBusinessId());

        tbBusinessTrackRecord.setCreateBy(username);
        tbBusinessTrackRecord.setCreateTime(createDate);

        tbBusinessMapper.updateTbBusiness(tbBusiness);
        tbBusinessTrackRecordMapper.insert(tbBusinessTrackRecord);
    }
    @Autowired
    private TbBusinessTrackRecordMapper businessTrackRecordMapper;
    @Override
    public List<TbBusinessTrackRecord> list(Long id) {
        List<TbBusinessTrackRecord> list = businessTrackRecordMapper.list(id);
        return list;
    }

    @Override
    public void updateStatus(Long id, String reson) {
        String dictValue = dictDataMapper.selectDictValue("reasons_for_business_reporting", reson);
        tbBusinessMapper.resetNextTimeAndStatus(id,TbClue.StatusType.FALSE.getValue());
        TbBusinessTrackRecord tbBusinessTrackRecord = new TbBusinessTrackRecord();
        tbBusinessTrackRecord.setBusinessId(id);
        tbBusinessTrackRecord.setRecord(dictValue);
        tbBusinessTrackRecord.setTrackStatus(TbClue.StatusType.FALSE.getValue());
        tbBusinessTrackRecord.setNextTime(null);
        tbBusinessTrackRecord.setCreateTime(new Date());
        tbBusinessTrackRecord.setCreateBy(SecurityUtils.getUsername());
        businessTrackRecordMapper.insert(tbBusinessTrackRecord);
    }
}
